package com.zcc.makingfriend.controller;

import com.zcc.makingfriend.dao.Category;
import com.zcc.makingfriend.dto.PageDto;
import com.zcc.makingfriend.service.CategoryService;
import com.zcc.makingfriend.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zcc
 */
@Api(tags = "分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @ApiOperation("查询所有分类")
    @GetMapping("/getAllCategory")
    public Result getAllCategory(){
        List<Category> allCategory = categoryService.findAllCategory();
        return Result.success(allCategory);
    }

    @ApiOperation("查询已经存在的分类")
    @GetMapping("/getCategory")
    public Result getCategory(){
        List<Category> allCategory = categoryService.findCategory();
        return Result.success(allCategory);
    }

    @ApiOperation("新增分类")
    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody Category category){
        int i = categoryService.addCategory(category);
        if (i == 1) {
            return Result.success("新增分类成功");
        } else if (i == -1) {
            return Result.fail("新增的标题重复");
        }
        return Result.fail("新增分类失败");
    }

    @ApiOperation("更新分类")
    @PutMapping("/updateCategory")
    public Result updateCategory(@RequestBody Category category){
        int i = categoryService.updateCategory(category);
        if (i == 1) {
            return Result.success("修改分类成功");
        }
        return Result.fail("修改分类失败");
    }

    @ApiOperation("删除分类")
    @PutMapping("/deleteCategory/{id}")
    public Result deleteCategory(@PathVariable Long id){
        int i = categoryService.deleteCategory(id);
        if (i == 1) {
            return Result.success("删除分类成功");
        }
        return Result.fail("删除分类失败");
    }

    @ApiOperation("通过标题查找分类")
    @GetMapping("/findCategoryByTitle/{title}")
    public Result findCategoryByTitle(@PathVariable String title) {
        Category category = categoryService.findCategoryByTitle(title);
        if (category != null) {
            return Result.success(category);
        }
        return Result.fail("未查找到该分类");
    }

    @ApiOperation("分页查看分类")
    @GetMapping("/findCategoryByPage/{page}/{size}")
    public Result findCategoryByPage(@PathVariable int page, @PathVariable int size) {
        List<Category> categoryByPage = categoryService.findCategoryByPage(page, size);
        return Result.success(categoryByPage);
    }

    @ApiOperation("统计分类数量")
    @GetMapping("/countCategory")
    public Result countCategory() {
        Long i = categoryService.countCategory();
        return Result.success(i);
    }

    @ApiOperation("模糊查询分类")
    @PostMapping("/byCondition")
    public Result findCategoryByCondition(@RequestBody PageDto pageDto) {
        List<Category> category = categoryService.findCategoryByCondition(pageDto);
        return Result.success(category);
    }

    @ApiOperation("模糊查询分类数量")
    @PostMapping("/countByCondition")
    public Result countCategoryByCondition(@RequestBody PageDto pageDto) {
        Long category = categoryService.countCategoryByCondition(pageDto);
        return Result.success(category);
    }
}
